<%@ page language="java"  contentType="text/html;charset=UTF-8"%>
<%@ include file="/commons/taglibs.jsp"%>
<html>
<head>
<title>组织/单位管理</title>
<%@ include file="/commons/meta.jsp" %>
<%@ include file="/commons/header.jsp" %>
<%@ include file="/extjs/jsps/importExt.jsp" %>
<%@ include file="/extjs/jsps/importEasyExt.jsp" %>
<script language="javascript" type="text/javascript">
var checkedNode;

var treePanel = new Ext.tree.TreePanel({
	title: '组织/单位',
	style: 'text-align: left',
	autoScroll: true,
	enableDD: true,
	containerScroll: true,
	rootVisible: true,
	animate: true,
    root: new Ext.tree.AsyncTreeNode({
		id: '1',
		text: '${ROOT_NAME}',
		leaf: false,
		expanded: true
    }),
	loader: new Ext.tree.TreeLoader({
		url: '${ctx}/org!listJSON.action'
	})
});

function appendChild(obj) {
	checkedNode.leaf=false;
	var newNode=new Ext.tree.AsyncTreeNode({
		id: obj.id,
		text: obj.name,
		leaf: true,
		orderNum: obj.orderNum
	});
	checkedNode.appendChild(newNode);
}

function setTreeNode(obj) {
	if(obj.name) {
		checkedNode.setText(obj.name);
	}
}

var rightClick = new Ext.menu.Menu({
	items: [{
		iconCls: 'addBtn',
		text: '添加',
		handler: function() { 
			var paraUrl="&parentId="+checkedNode.id;
			var url="${ctx}/org!input.action?winId=winInsertOrg"+paraUrl;
			windowOpen(url,'winInsertOrg','新增组织/单位',205,355,'','',{
				onClose: function(scope) {
					var obj=scope.returnValue;
					if(obj) {
						appendChild(obj);
					}
				}
			});
		} 
	},{
		iconCls: 'editBtn',
		text: '编辑',
		handler: function() {
			var paraUrl="&id="+checkedNode.id;
			if(checkedNode.id!='1') {
				paraUrl+="&parentId="+checkedNode.parentNode.id;
			}
			var url="${ctx}/org!input.action?winId=winUpdateOrg"+paraUrl;
			windowOpen(url,'winUpdateOrg','更新组织/单位',205,355,'','',{
				onClose: function(scope) {
					var obj=scope.returnValue;
					if(obj) {
						setTreeNode(obj);
					}
				}
			});
		}
	},{
		iconCls: 'deleteBtn',
		text: '删除',
		handler: function() {
			Ext.Msg.confirm('',(checkedNode.isLeaf()?'':'会同时删除下级组织/单位，')+'确认删除？',function(e) {
				if (e!='yes') return;
				Ext.Ajax.request({
					url: '${ctx}/org!deleteJSON.action',
					params: {
						id: checkedNode.id
					},
					failure: function(response,opts) {
						Ext.Msg.hide();
			        	Common.parseFailureResponse(response);
					},
					success: function(response,opts) {
						var result=Ext.decode(response.responseText);
						Common.parse(result,function(buttonId) {
							Ext.Msg.hide();
							if(result.pass||buttonId=='yes') {
								checkedNode.remove();
							}
						});
					}
				});
			});
		} 
	}]
});

treePanel.on('contextmenu',function(node,event) {
	event.preventDefault();// 阻止浏览器默认右键菜单显示
	
	rightClick.get(0).show();// 添加
	if(node.id=='2') {
		rightClick.get(1).hide();// 编辑
	}else {
		rightClick.get(1).show();// 编辑
	}
	if(node.id=='1'||node.id=='2') {
		rightClick.get(2).hide();// 删除
	}else {
		rightClick.get(2).show();// 删除
	}
	node.select();
	checkedNode=node;
	rightClick.showAt(event.getXY());// 取得鼠标点击坐标，展示菜单 
}); 

treePanel.on('nodedrop',function(e) { 
	var obj=new Object();
	obj.id=e.dropNode.id;
	obj.parentId=e.target.parentNode.id;
	if(e.point=='append') {
		obj.parentId=e.target.id;
	}else if(e.point=='above') {
		obj.orderNum=e.target.attributes.orderNum;
	}else if(e.point=='below') {
		if(e.dropNode.attributes.orderNum>e.target.attributes.orderNum) {
			obj.orderNum=e.target.attributes.orderNum+1;
		}else {
			obj.orderNum=e.target.attributes.orderNum;
		}
	}
	Common.ajaxRequest('${ctx}/org!sortJSON.action',obj,Ext.emptyFn,false);
});

Ext.onReady(function() {
	var viewport = new Ext.Viewport({
		layout: 'fit',
		items: [treePanel]
	})
});
</script>
</head>

<body>
</body>
</html>
